/**
 * @Auther: ayo
 * @Date: 2018/8/22 15:57
 * @Description: 警告框alert工具类
 */
import { Injectable } from '@angular/core';
import { AlertController } from '@ionic/angular';

@Injectable()
export class AlertProvider {

    private alertWindow;

    constructor(private alertController: AlertController) {
    }

    dismiss = () => {
        if (this.alertWindow) {
            this.alertWindow.dismiss();
            this.alertWindow = null;
        }
    };

    /**
     * @Description: // 用于自定义alert
     **/
    showCommonAlert(opts?) {
        if (!opts) {
            opts = {
                title: '消息提示', buttons: ['关闭']
            };
        }
        this.alertWindow = this.alertController.create(opts);
        this.alertWindow.present();
    }

    presentAlert(title = '消息提示', subTitle = '') {
        const opts = {
            title: title, subTitle: subTitle, buttons: ['取消']
        };
        this.alertWindow = this.alertController.create(opts);
        this.alertWindow.present();
    }

    presentConfirm(title = '确定提示', message = '', confirmText?: string, cancelText?: string, confirmCallback?: Function,
                   cancelCallback?: Function) {
        const buttons: any[] = [{
            text: cancelText || '取消', role: 'cancel', handler: cancelCallback
        }, {
            text: confirmText || '确定', handler: confirmCallback
        }];
        const option = {
            title: title, message: message, buttons: buttons
        };
        this.alertWindow = this.alertController.create(option);
        this.alertWindow.present();
    }

    /**
     * alert弹窗
     * http://ionicframework.com/docs/api/components/alert/AlertController/
     */
    alert(content, callback = () => {
    }, dismissFlag          = false, dismissCallback = null) {
        this.alertWindow = this.alertController.create({
            header: '<span>温馨提示</span>',
            message: '<div>' + content + '</div>',
            backdropDismiss: false,
            cssClass: '',
            buttons: [{
                text: '好的', cssClass: 'pop_btn', handler: () => {
                    if (callback !== undefined && callback !== null && typeof callback === 'function') {
                        callback();
                    }
                }
            }]
        });
        this.alertWindow.present();
        if (dismissFlag) {
            setTimeout(() => {
                this.dismiss();
                if (undefined !== dismissCallback && null !== dismissCallback && 'function' === typeof dismissCallback) {
                    dismissCallback();
                }
            }, 3000);
        }
    }

    /**
     * 自定义alert弹窗-
     */
    alertDIY(obj, ok_callback: any = () => {
    }) {
        // enableBackdropDismiss: 是否点击背景关闭弹窗
        this.alertWindow = this.alertController.create({
            header: obj.header || '<div class="content_img">温馨提示</div>',
            subHeader: obj.subHeader,
            message: obj.message,
            cssClass: obj.cssClass,
            backdropDismiss: false,
            buttons: [{
                text: obj.okText || '确定', cssClass: 'pop_btn', handler: () => {
                    if (ok_callback !== undefined && ok_callback !== null && typeof ok_callback === 'function') {
                        ok_callback();
                    }
                }
            }]
        });
        this.alertWindow.present();
    }

    /**
     * 拨打号码弹窗
     */
    callPop(obj, esc_callback: any = () => {
    }, ok_callback: any = () => {
    }) {
        setTimeout(() => {
            // 延迟几秒可以等html反应，这样获取的高度才准确
            const test = document.getElementsByClassName('pop_btn')[0];
            test.innerHTML = '<a href=\'tel:' + obj.tel + '\'> 继续呼叫 </a>';
        }, 3);
        this.alertWindow = this.alertController.create({
            header: obj.header || '<div class="content_img"><img src="./assets/img/community.png"/></div>',
            subHeader: obj.subHeader,
            cssClass: 'call',
            message: '<div>' + obj.message + '</div>',
            backdropDismiss: false,
            buttons: [{
                text: obj.escText || '取消', handler: () => {
                    if (esc_callback !== undefined && esc_callback !== null && typeof esc_callback === 'function') {
                        esc_callback();
                    }
                }
            }, {
                text: obj.okText || '确定', cssClass: 'pop_btn', handler: () => {
                    if (ok_callback !== undefined && ok_callback !== null && typeof ok_callback === 'function') {
                        ok_callback();
                    }
                }
            }]
        });
        this.alertWindow.present();
    }
}
